package com.icedevcloud.upms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icedevcloud.common.core.exception.ApiException;
import com.icedevcloud.upms.controller.admin.dept.dto.AddDeptReqDto;
import com.icedevcloud.upms.controller.admin.dept.dto.DeptQueryReqDto;
import com.icedevcloud.upms.controller.admin.dept.dto.UpdateDeptReqDto;
import com.icedevcloud.upms.entity.SysDept;
import com.icedevcloud.upms.mapper.SysDeptMapper;
import com.icedevcloud.upms.service.ISysDeptService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author XiaoBingBy
 * @since 2023-08-13
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public List<SysDept> deptList(DeptQueryReqDto reqDto) {
        return this.list(
                Wrappers.<SysDept>lambdaQuery()
                        .eq(StrUtil.isNotBlank(reqDto.getDeptName()), SysDept::getDeptName, reqDto.getDeptName())
                        .eq(reqDto.getStatus() != null, SysDept::getStatus, reqDto.getStatus())
                        .orderByAsc(SysDept::getSort)
        );
    }

    @Override
    public Boolean add(AddDeptReqDto reqDto) {
        SysDept sysDept = BeanUtil.copyProperties(reqDto, SysDept.class);
        return this.save(sysDept);
    }

    @Override
    public Boolean update(UpdateDeptReqDto reqDto) {
        SysDept sysDept = this.getById(reqDto.getId());
        if (sysDept == null) {
            throw new ApiException("部门不存在");
        }
        BeanUtil.copyProperties(reqDto, sysDept);
        return this.updateById(sysDept);
    }

    @Override
    public Boolean delete(Long id) {
        SysDept sysDept = this.getById(id);
        if (sysDept == null) {
            throw new ApiException("部门不存在");
        }
        return this.removeById(id);
    }

}
